package com.gao.util;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author gao
 * @date 2022/11/29 16:46:15
 */
public class WebUtils {
    public static void renderString(HttpServletResponse response, String string) throws IOException {
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(string);
    }

    public static String generateSuccessJson(String msg) {
        ResultVO vo = ResultVO.success(msg, null);
        return JSON.toJSONString(vo);
    }

    public static String generateSuccessJson(String msg, Object data) {
        ResultVO vo = ResultVO.success(msg, data);
        return JSON.toJSONString(vo);
    }

    public static String generateFailureJson(String msg) {
        ResultVO vo = ResultVO.failure(msg, null);
        return JSON.toJSONString(vo);
    }

    public static String generateFailureJson(String msg, Object data) {
        ResultVO vo = ResultVO.failure(msg, data);
        return JSON.toJSONString(vo);
    }
}
